package ph.com.isla.sf2d.ui;

import ph.com.isla.sf2d.SF2DSurfaceView;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Rect;

public class SF2DDialog extends SF2DObject {
	private String mMessage;
	private Paint mDimPaint = null;
	private Rect mDimRect = null;
	private Rect mDialogRect = null;
	private Rect mTitleBarRect = null;
	private Paint mDialogPaint = null;
	private int mDialogColor = Color.argb(128, 255, 255, 255);
	private Rect mMsgBounds = null;
	private Paint mMsgPaint;

	private boolean mModal = true;
	private boolean mShowTitleBar;
	private String mTitle;

	// default values
	int mDefaultTitleBarHeight;
	int mDefaultTitleBarTextColor;
	int mDefaultTitleBarBacgroundColor;
	int mDefaultDimColor;

	public void setShowTitleBar(boolean show) {
		mShowTitleBar = show;
	}

	public boolean isShowTitleBar() {
		return mShowTitleBar;
	}

	public void setModal(boolean modal) {
		mModal = modal;
	}

	public boolean isModal() {
		return mModal;
	}

	public void setTitle(String title) {
		mTitle = title;
	}

	public String getTitle() {
		return mTitle;
	}

	Paint getMessagePaint() {
		return mMsgPaint;
	}

	Rect getMessageBounds() {
		return mMsgBounds;
	}

	Rect getDimRect() {
		return mDimRect;
	}

	Rect getDialogRect() {
		return mDialogRect;
	}

	Paint getDialogPaint() {
		return mDialogPaint;
	}

	public void setMessage(String message) {
		mMessage = message;
		initializeMessageBounds();
	}

	public String getMessage() {
		return mMessage;
	}

	private void initializeMessageBounds() {
		if (mMessage != null) {
			mMsgPaint.getTextBounds(mMessage, 0, mMessage.length(), mMsgBounds);
			int width = (int) (mMsgBounds.width() * 3.5);
			int height = (int) (mMsgBounds.height() * 4);

			int left = (int) ((getSurface().getScaledWidth() - width) / 2);
			int top = (int) ((getSurface().getScaledHeight() - height) / 2);
			mDialogRect.set(left, top, (left + width), (top + height));
		}
	}

	private void setDefaultValues(){
		mDefaultHeight = getSurface().getScaledHeight() / 4;
		mDefaultWidth = (int) (getSurface().getScaledWidth() * 0.75);
		mDefaultTitleBarHeight = (int) (mDefaultHeight * 0.20);
		mDefaultDimColor = Color.argb(128, 00, 00, 00);
		mDefaultLeft  =  (getSurface().getScaledWidth() - mDefaultWidth) / 2;
		mDefaultTop = (getSurface().getScaledHeight() - mDefaultHeight) / 2;
		mDefaultBackgroundColor = Color.argb(192, 255, 255, 255);
		
		setLeft(mDefaultLeft);
		setTop(mDefaultTop);
		setWidth(mDefaultWidth);
		setHeight(mDefaultHeight);
		setBackgroundColor(mDefaultBackgroundColor);
	}
	private void initialize() {
		setDefaultValues();
		
		if (mModal) {
			mDimPaint = new Paint();
			mDimPaint.setColor(mDefaultDimColor);
			mDimRect = new Rect(0, 0, getSurface().getWidth(), getSurface()
					.getHeight());
		}

		mMsgPaint = new Paint();
		mMsgPaint.setTextSize(mDefaultTextSize);
		mMsgPaint.setColor(mDefaultTextColor);
		mMsgPaint.setTextAlign(Align.CENTER);
		mMsgPaint.setAntiAlias(true);

		mMsgBounds = new Rect();

		// draw dialog rectangle
		mDialogRect = new Rect();
		mDialogPaint = new Paint();
		mDialogPaint.setStyle(Style.STROKE);
		mDialogPaint.setColor(Color.BLACK);
		mDialogPaint.setStrokeWidth(3);

		SF2DBorder border = new SF2DBorder(mDefaultBorderWidth, mDefaultBorderColor);
		setBorder(border);
	}

	Paint getDimPaint() {
		return mDimPaint;
	}

	public SF2DDialog(SF2DSurfaceView root) {
		super(root);
		root.addSF2DDialog(this, 0, 0);
		initialize();
	}

	@Override
	public boolean isClickable() {
		return true;
	}

	public void show() {
		setVisible(true);
		getSurface().putSF2DObjectOnTop(this);
		getSurface().postInvalidate();
	}

	public void dismiss() {
		setVisible(false);
		getSurface().removeSF2DObject(this);
		getSurface().postInvalidate();
	}

	@Override
	public void draw(Canvas canvas) {
		if (mModal) 
			canvas.drawRect(mDimRect, mDimPaint);
		
		super.draw(canvas);
	}

	@Override
	public void onKeyDown(float x, float y) {

	}

	@Override
	public void onKeyUp(float x, float y) {

	}

	@Override
	public void onMoveOut(float x, float y) {

	}

	@Override
	public Rect getClickableArea() {
		return null;
	}

}
